package com.waiterxiaoyy.backandroiddesign.controller;


import com.waiterxiaoyy.backandroiddesign.Service.DakaService;
import com.waiterxiaoyy.backandroiddesign.entity.DaKa;
import com.waiterxiaoyy.backandroiddesign.utils.vo.DataVo;
import io.swagger.annotations.Api;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2021/1/14 22:37
 */
@Api(description = "打卡控制")
@RestController
@RequestMapping("/api")
public class DakaController {
    @Autowired
    private DakaService dakaService;

    @GetMapping("addka/{filename}")
    public DataVo addKa(@PathVariable("filename") String fileName) {
        return dakaService.addKa(fileName);
    }

    @GetMapping("export")
    public void downloadAllClassmate(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

        List<DaKa> daKaList = dakaService.export();

        String fileName = "daka"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "年级", "姓名", "学号", "班级", "联系方式", "未打卡次数", "是否党员"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (DaKa daKa : daKaList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(daKa.getGrade());
            row1.createCell(1).setCellValue(daKa.getStudent());
            row1.createCell(2).setCellValue(daKa.getStudentid());
            row1.createCell(3).setCellValue(daKa.getClassname());
            row1.createCell(4).setCellValue(daKa.getPhone());
            row1.createCell(5).setCellValue(daKa.getNum());
            row1.createCell(6).setCellValue(daKa.getDang());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
